<template>
  <div class="dashboard-container">
    <div class="app-container">
      <page-tools :show-before="true">
        <span slot="before">共{{ page.total }}条记录</span>
        <template v-slot:after>
          <el-button size="small" type="warning">导入</el-button>
          <el-button size="small" type="danger">导出</el-button>
          <el-button size="small" type="primary" @click="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>
      <!-- 放置表格加分页 -->
      <el-table v-loading="loading" border :data="list">
        <el-table-column label="序号" sortable type="index" />
        <el-table-column label="姓名" sortable prop="username" />
        <el-table-column label="工号" sortable prop="workNumber" />
        <el-table-column label="头像" sortable width="200">
          <template slot-scope="{row}">
            <img :src="row.staffPhoto" alt="" style="width:100%">
          </template>
        </el-table-column>
        <el-table-column label="聘用形式" sortable prop="formOfEmployment" :formatter="formatEmployment" />
        <el-table-column label="部门" sortable prop="departmentName" />
        <el-table-column label="手机号" sortable prop="mobile" />
        <el-table-column label="入职时间" sortable>
          <template v-slot="{row}">
            {{ row.timeOfEntry | formatDate }}
          </template>
        </el-table-column>
        <el-table-column label="账户状态状态" sortable>
          <template v-slot="{row}">
            <el-switch :value="row.enableState === 1" />
          </template>
        </el-table-column>
        <el-table-column label="操作" sortable fixed="right" width="280">
          <template slot-scope="{row}">
            <el-button size="small" type="text" :disabled="!checkPermission('POINT-USER-UPDATE')" @click="$router.push(`/employees/detail/${row.id}`)">查看</el-button>
            <el-button size="small" type="text">转正</el-button>
            <el-button size="small" type="text">调岗</el-button>
            <el-button size="small" type="text">离职</el-button>
            <el-button size="small" type="text">调岗</el-button>
            <el-button size="small" type="text" @click="editRole(row.id)">角色</el-button>
            <el-button size="small" type="text" @click="deleteEmployee(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height:60px">
        <el-pagination :current-page="page.page" :page-size="page.size" :total="page.total" layout="prev,pager,next" @current-change="changePage" />
      </el-row>
    </div>
    <!-- 放置组件弹层 -->
    <add-employee :show-dialog.sync="showDialog" @addEmployee="getEmployeeList" />
    <!-- 放置分配角色弹框 -->
    <assign-role ref="assignRef" :show-role-dialog.sync="showRoleDialog" :user-id="userId" />
  </div>
</template>

<script>
import { getEmployeeList, deleteEmployee } from '@/api/employeement'
import EmployeeEnum from '@/api/constant/employees.js'
import addEmployee from '@/views/employees/components/add-employee.vue'
import assignRole from '@/views/employees/components/assign-role.vue'
export default {
  components: {
    addEmployee,
    assignRole
  },
  data() {
    return {
      list: [],
      page: {
        page: 1,
        size: 10,
        total: 0
      },
      loading: false,
      // 弹层是否显示
      showDialog: false,
      // 分配角色弹框是否显示
      showRoleDialog: false,
      // 用户id
      userId: null

    }
  },
  created() {
    this.getEmployeeList()
  },
  methods: {
    async getEmployeeList() {
      this.loading = true
      const { total, rows } = await getEmployeeList(this.page)
      this.page.total = total
      this.list = rows
      this.loading = false
    },
    // 当前页码改变
    changePage(newPage) {
      this.page.page = newPage
      this.getEmployeeList()
    },
    // 格式化聘用形式
    formatEmployment(row, column, cellValue, index) {
      const obj = EmployeeEnum.hireType.find(item => item.id === cellValue)
      return obj ? obj.value : '未知'
    },
    // 删除员工
    async deleteEmployee(id) {
      try {
        await this.$confirm('确定删除该员工吗?')
        // 点击了确定
        await deleteEmployee(id)
        this.$message.success('删除员工成功')
        this.getEmployeeList()
      } catch (err) {
        console.log(err)
      }
    },
    // 分配角色
    async editRole(id) {
      this.userId = id
      await this.$refs.assignRef.getUserDetailByid(id)
      this.showRoleDialog = true
    }
  }
}
</script>

<style>

</style>
